//Readable,Writable and Transform Streams

//1.create 2 streams for read and write
const fs = require('fs');
const readStream = fs.createReadStream('File Name.txt','utf8');
const writeStream = fs.createWriteStream('File Name2.txt');

//2.write a file from what it reads.
readStream.on('data',(chunk)=>{
    writeStream.write(chunk);
    console.log('...');
});

readStream.pipe(writeStream);//与上面方法等效。

//3.Pipe Chaining, create a transform stream. Compress
const zlib = require('zlib');//for basically compression, uncompression files.
const gzip = zlib.createGzip();//in this case, a transform stream receives data and manipulates the data into something else.

const writeStream3 = fs.createWriteStream('Compress.txt.gz');//create a destination

readStream.pipe(gzip).pipe(writeStream3);//compress

//4.Uncompress. To test this sample, the step 1,2,3 should be run in advance, or the uncompressed.txt would be empty.
const readStream4 = fs.createReadStream('Compress.txt.gz');
const writeStream4 = fs.createWriteStream('uncompressed.txt');

const gunzip = zlib.createGunzip();
readStream4.pipe(gunzip).pipe(writeStream4);//uncompress